package com.gxs.ea.fileupload.beans.jsf.company;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.gxs.ea.fileportal.domain.BaseCompany;
import com.gxs.ea.fileupload.beans.data.service.CompanyService;

@Component
@Scope("session")
public class CreateCompanyBean  implements Serializable
{
	private BaseCompany company;
	private boolean viewParameterInit = false;
	
	@Autowired
	private CompanyService companyService;
	
	private void clearFields()
	{
		this.company = null;
	}
	
	public void setViewParameterInit(boolean value)
	{
		this.viewParameterInit = value;
		if(value)
		{
			this.initialize();
		}
	}

	public BaseCompany getCompany() {
		return company;
	}

	public void setCompany(BaseCompany company) {
		this.company = company;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public boolean isViewParameterInit() {
		return viewParameterInit;
	}
	
	public void createCompany()
	{
		try
		{
			company= this.companyService.saveCompany(company);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Creation Successful", "BaseCompany creatd successfully with id: " + company.getId() ) );
			initialize();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Creation Failed", "BaseCompany creation failed with error: " + e.getMessage()));
		}
	}
	
	private void initialize()
	{
		this.company = new BaseCompany(true);
	}
}
